package com.chixing.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;


public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    private static SqlSession sqlSession;

    /**
     * static: 在类加载的时候执行，只执行一次
     *  sqlSessionFactory 重量级资源，
     */
    static{
        InputStream is = MybatisUtil.class.getResourceAsStream("/config/mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    }
    public static SqlSession getSqlSession(){
        sqlSession =  sqlSessionFactory.openSession();
        return sqlSession;
    }
    public static void  closeSqlSession(){
        if(sqlSession !=null)
            sqlSession.close();
    }


}
